import QtQuick 2.12
import QtQuick.Controls 2.5
import QtQuick.Window 2.12
import QtQuick.Layouts 1.12
import AIClient.module 1.0
Page {
    signal createModel(string model_name, string model_class, string dependencies, string content, string description, string stype, string smodel_file_name, int pb, string commit)
    signal editModel(string uid, string model_name, string model_class, string dependencies, string content, string description, string stype, string smodel_file_name, int pb, string commit)
    signal cancel()

    function updateMode() {
        if ( m_mode === 0 )  //bianji
        {
            model_name.enabled = true
            description.enabled = true
            model_class.enabled = true
            dependencies.enabled = true
            publicCombo.visible = false
            publicCombo.enabled = false
            commit.visible = true
            commit.enabled = true
            commit_tip.visible = true
            model_public_tip.visible = false
            content_tip.visible = true
            content.visible = true
            content.enabled = true
            //content.parent.visible = true
            //content_back.visible = true
            btn.visible = true
            version.visible = true
            version.enabled = false
            version_tip.visible = true
            update()
        }
        else if ( m_mode === 1 ) //xinjian
        {
            model_name.enabled = true
            description.enabled = true
            model_class.enabled = true
            dependencies.enabled = true
            publicCombo.visible = true
            publicCombo.enabled = true
            model_public_tip.visible = true
            content_tip.visible = true
            content.visible = true
            content.enabled = true
            //content.parent.visible = true
            //content_back.visible = true
            btn.visible = true
            commit.visible = true
            commit.enabled = true
            commit_tip.visible = true
            version.visible = false
            version.enabled = false
            version_tip.visible = false
            update()
        }
        else if ( m_mode === 2 ) //chakan
        {
            model_name.enabled = false
            description.enabled = false
            model_class.enabled = false
            dependencies.enabled = false
            content_tip.visible = true
            content.visible = true
            content.enabled = false
            publicCombo.visible = true
            publicCombo.enabled = false
            model_public_tip.visible = true
            //content.parent.visible = false
            //content_back.visible = false
            btn.visible = false
            commit.visible = false
            commit.enabled = false
            commit_tip.visible = false
            version.visible = true
            version.enabled = false
            version_tip.visible = true
            update()
        }
    }
    ListModel {
        /* 数据项 */
        id: publicModel
        ListElement {
            pb: 0;
            text: "否";
            color: "black";
        }
        ListElement {
            pb: 1;
            text: "是";
            color: "black";
        }
    }
//    AIClient {
//        id: client
//    }

//    TipMessage {
//        id: tip
//        width: parent.width
//    }

//    function setToken(token) {
//        m_token = token
//    }

    id: model_create
    title: "创建模型"
    visible: false
    //visibility: "Maximized"
//    modality: Qt.ApplicationModal
//    minimumWidth: Screen.desktopAvailableWidth*0.3
//    maximumWidth: Screen.desktopAvailableWidth
//    minimumHeight: Screen.desktopAvailableHeight*0.6
//    maximumHeight: Screen.desktopAvailableHeight
    width: parent.width*0.8
    height: parent.height*0.8
    property int m_mode: 1
//    property string m_token: ""
    property string m_uid: ""
    property string m_model_name: ""
    property string m_model_class: ""
    property string m_dependencies: ""
    property string m_content: ""
    property string m_description: ""
    property string m_btn: ""
    property int m_pb: 0
    property int m_from: 0

    property string m_version: ""
    property string m_commit: ""
  //  color: "azure"
//
//    Flickable {
//        id: flickable
//        clip: true
//        anchors.fill: parent
//        Keys.onUpPressed: scrollBar.decrease()
//            Keys.onDownPressed: scrollBar.increase()
//        // ...


    Rectangle {
        anchors.fill: parent
        color:"azure"
        width: model_create.width
        height: model_create.height
        GridLayout {
//            ScrollBar.vertical: ScrollBar {
//                id: scrollBar
//                parent: flickable.parent
//                anchors.top: flickable.top
//                anchors.left: flickable.right
//                anchors.bottom: flickable.bottom
//            }

            id: grid_modelcreate
            rows: 10
            columns: 2
            anchors.topMargin: model_create.height*0.1
            anchors.bottomMargin: model_create.height*0.1
            anchors.leftMargin: model_create.width*0.1
            anchors.rightMargin: model_create.width*0.1
            anchors.fill: parent
            columnSpacing: 5
            rowSpacing: 15
            Text {
                text: qsTr("名称")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.maximumWidth: 200
                Layout.preferredWidth: 180
                color: "black"
            }
            TextField {
                id: model_name
                text: m_model_name
                selectByMouse: true
                font.pointSize: 12
                Layout.minimumHeight: 12*2
                Layout.preferredHeight: 50
                //Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                Layout.fillWidth: true
            }
            Text {
                text: qsTr("模型类名")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                color: "black"
            }
            TextField {
                id: model_class
                text: m_model_class
                selectByMouse: true
                font.pointSize: 12
                Layout.minimumHeight: 12*2
    //            passwordCharacter: "*"
                Layout.preferredHeight: 50
                //Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                Layout.fillWidth: true
            }
            Text {
                text: qsTr("依赖")
                font.pointSize: 12
                font.bold: false
                Layout.fillHeight: true
                Layout.preferredHeight: 50
                //Layout.fillWidth: true
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 200
                Layout.minimumHeight: 60
                Layout.maximumHeight: 400
                //height: 500
                Layout.fillHeight: true
                Layout.fillWidth: true

                id: dependencies
            }
//            ScrollView{
//                //Layout.minimumWidth: 500
//                Layout.fillHeight: true
//                Layout.preferredHeight: 200
//                Layout.minimumHeight: 12*2
//                Layout.fillWidth: true
//                TextArea {
//                    id: dependencies
//                    text: m_dependencies
//                    selectByMouse: true
//                    font.pointSize: 12
//                    background: Rectangle{
//                        anchors.fill: parent
//                        //width: 500
//                        color: "white"
//                    }
//                }
//            }
            Text {
                text: qsTr("描述")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.fillHeight: true
                //Layout.fillWidth: true
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 200
                Layout.minimumHeight: 80
                Layout.maximumHeight: 400
                //height: 500
                Layout.fillHeight: true
                Layout.fillWidth: true

                id: description
            }
//            ScrollView{
//                //Layout.minimumWidth: 500
//                Layout.preferredHeight: 200
//                Layout.minimumHeight: 12*3
//                Layout.fillHeight: true
//                Layout.fillWidth: true
//                TextArea {
//                    id: description
//                    text: m_description
//                    selectByMouse: true
//                    font.pointSize: 12

//                    background: Rectangle{
//                        anchors.fill: parent
//                        //width: 500
//                        color: "white"
//                    }
//                }
//            }
    //        TextField {
    //            id: dependencies
    //            text: m_dependencies
    //            Layout.preferredHeight: 40

    //        }
            Text {
                id: content_tip
                text: qsTr("内容")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.fillHeight: true
                //Layout.fillWidth: true
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 500
                Layout.minimumHeight: 12*7
                Layout.maximumHeight: 500
                //height: 500
                //Layout.fillHeight: true
                Layout.fillWidth: true

                id: content
            }
            Text {
                id: commit_tip
                text: qsTr("签入信息")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.fillHeight: true
                //Layout.fillWidth: true
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 50
                Layout.minimumHeight: 60
                Layout.maximumHeight: 60
                //height: 500
                //Layout.fillHeight: true
                Layout.fillWidth: true

                id: commit
            }
            Text {
                id: version_tip
                text: qsTr("版本")
                font.pointSize: 12
                Layout.preferredHeight: 50
                Layout.fillHeight: true
                //Layout.fillWidth: true
                color: "black"
            }
            ScrollTextView {
                Layout.preferredHeight: 50
                Layout.minimumHeight: 60
                Layout.maximumHeight: 60
                //height: 500
                //Layout.fillHeight: true
                Layout.fillWidth: true

                id: version
            }
            Text {
                id: model_public_tip
                text: qsTr("模型公开")
                font.pointSize: 12
                font.bold: false
                Layout.preferredHeight: 50
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                Layout.maximumWidth: 200
                Layout.preferredWidth: 180
                color: "black"
            }
            ComboBox {
                id: publicCombo
                currentIndex: 1
//                Layout.column: 1
//                Layout.row: 2
                font.pointSize: 12
                Layout.minimumHeight: 12*3
                Layout.preferredHeight: 50
                Layout.preferredWidth: 500
                Layout.maximumHeight: 60
                //                Layout.fillHeight: true
                Layout.fillWidth: true
                model: publicModel
                textRole: "text"
            }
//            Rectangle {
//                //Layout.minimumWidth: 500
//                Layout.preferredHeight: 500
//                Layout.minimumHeight: 12*7
//                Layout.maximumHeight: 500
//                //height: 500
//                //Layout.fillHeight: true
//                Layout.fillWidth: true
//                color: "white"

//            ScrollView{
//                anchors.fill: parent
//                TextArea {
//                    id: content
//                    anchors.fill: parent
//                    anchors.leftMargin: 15
//                    anchors.rightMargin: 15
//                    anchors.topMargin: 15
//                    anchors.bottomMargin: 15
//                    text: m_content
//                    selectByMouse: true
//                    font.pointSize: 12

//                    background: Rectangle{
//                        id: content_back
//                        //anchors.fill: parent
//                        //width: 500
//                        color: "transparent"
//                    }
//                }
//            }
//            }
            Rectangle{
                Layout.row: 6
                Layout.column: 0
                Layout.preferredHeight: 40
                Layout.maximumHeight: 60
                //Layout.fillWidth: true
                color:"transparent"
            }

            RowLayout {
                Layout.row: 8
                Layout.column: 1
                Layout.maximumHeight: 60
                Layout.fillWidth: true
            //Layout.preferredWidth: 500
                Layout.minimumHeight: 12*1.5
                OrangeButton {
                    id: btn
                    text:"保存"
                    Layout.minimumWidth: 120
                    Layout.maximumWidth: 180
                    onClicked: {
                    //client.setConfig(m_token)
                        if ( m_mode === 1) {
                            createModel(model_name.text, model_class.text, dependencies.text, content.text, description.text, "py", "", publicModel.get(publicCombo.currentIndex).pb, commit.text)
                        } else if ( m_mode === 0 ){
                            editModel(m_uid, model_name.text, model_class.text, dependencies.text, content.text, description.text, "py", "", publicModel.get(publicCombo.currentIndex).pb, commit.text)
                        }
                    }
                }
                OrangeButton {
                    text: "返回"
                    Layout.minimumWidth: 120
                    Layout.maximumWidth: 180
                    onClicked: {
                        cancel()
                    }
                }
            }
            Rectangle {
                Layout.row: 9
                Layout.column: 0
                Layout.fillHeight: true
                Layout.fillWidth: true
                color: "transparent"
                Layout.columnSpan: 3
            }


        }
    }

    Binding {
        target: commit
        property: "text"
        value: model_create.m_commit
    }
    Binding {
        target: model_create
        property: "m_commit"
        value: commit.text
    }

    Binding {
        target: version
        property: "text"
        value: model_create.m_version
    }
    Binding {
        target: model_create
        property: "m_version"
        value: version.text
    }

    Binding {
        target: model_create
        property: "m_model_name"
        value: model_name.text
    }
    Binding {
        target: model_name
        property: "text"
        value: model_create.m_model_name
    }
    Binding {
        target: model_create
        property: "m_model_class"
        value: model_class.text
    }
    Binding {
        target: model_class
        property: "text"
        value: model_create.m_model_class
    }
    Binding {
        target: model_create
        property: "m_dependencies"
        value: dependencies.text
    }
    Binding {
        target: dependencies
        property: "text"
        value: model_create.m_dependencies
    }
    Binding {
        target: model_create
        property: "m_content"
        value: content.text
    }
    Binding {
        target: content
        property: "text"
        value: model_create.m_content
    }
    Binding {
        target: model_create
        property: "m_description"
        value: description.text
    }
    Binding {
        target: description
        property: "text"
        value: model_create.m_description
    }
    Binding {
        target: model_create
        property: "m_btn"
        value: btn.text
    }
    Binding {
        target: btn
        property: "text"
        value: model_create.m_btn
    }
    Binding {
        target: publicCombo
        property: "currentIndex"
        value: m_pb
    }

}
